package activity.wheelOfFortune;

import enums.PrizeCancelType;
import enums.PrizeType;
import enums.ReceiveStatus;
import models.BaseModel;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import models.weixin.WebUser;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * Created by LiuBin on 2016/5/13.
 */
@Entity
@Table(name = "user_prizes")
public class UserPrizes extends BaseModel {

    /**
     * 奖项名称
     */
    @Column(name = "name")
    public String name;

    /**
     * 商户
     */
    @ManyToOne
    @JoinColumn(name = "merchant_id")
    public Merchant merchant;


    /**
     * 奖品信息
     */
    @ManyToOne
    @JoinColumn(name = "prize_item_id")
    public PrizeItem prizeProduct;


    /**
     * 关联的微信用户.
     */
    @ManyToOne
    @JoinColumn(name = "weixin_user_id")
    public WebUser weixinUser;


    /**
     * 抽奖时间
     */
    @Column(name = "prize_time")
    public Date prizeTime;

    /**
     * 奖品有效时间 , 天数
     */
    @Column(name = "valid_time")
    public Integer validTime ;

    /**
     * 奖品领取状态
     */
    @Column(name = "receive_status")
    @Enumerated(EnumType.STRING)
    public ReceiveStatus receiveStatus;

    /**
     * 创建时间
     */
    @Column(name = "receive_date")
    public Date receiveDate;

    /**
     * 奖品类型
     */
    @Column(name = "prize_type")
    @Enumerated(EnumType.STRING)
    public PrizeType prizeType;

    /**
     * 图片地址
     */
    @Column(name = "image_url")
    public String imageUrl;

    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     *
     * @param user
     * @param type
     * @param status
     * @return
     */
    public static List<UserPrizes> findByWeixinUser(WebUser user , PrizeType type , ReceiveStatus status) {
        return UserPrizes.find("weixinUser = ? and prizeType = ? and receiveStatus = ? and deleted = ?" , user , type , status , DeletedStatus.UN_DELETED).fetch();
    }

    /**
     *
     * @param userId
     * @param merchantId
     * @return
     */
    public static List<UserPrizes> loadByUserAndMechant(long userId , long merchantId){
        return UserPrizes.find(" weixinUser.id = ? and merchant.id = ? and deleted = ? order by prizeTime desc" , userId , merchantId , DeletedStatus.UN_DELETED).fetch();
    }

    /**
     * 查询用户在商城获得的未领取的实物奖品
     * @param userId
     * @param merchantId
     * @return
     */
    public static List<UserPrizes> loadUnreceivePrizes(long userId , long merchantId ){

        return UserPrizes.find(" weixinUser.id = ? and merchant.id = ?  and deleted = ? and receiveStatus = ? and prizeType = ? order by prizeTime desc" , userId , merchantId , DeletedStatus.UN_DELETED , ReceiveStatus.UNRECEIVE,PrizeType.SHIWU).fetch();
    }

    /**
     * 根据商户查询某一领取状态的奖品
     * @param merchantId 商户Id
     * @param receiveStatus 领取状态
     * @param prizeCancelType 奖品作废方式
     * @return
     */
    public static List<UserPrizes> loadByMerchantAndReceiveStatus(long merchantId ,ReceiveStatus receiveStatus , PrizeCancelType prizeCancelType ){
        return UserPrizes.find("deleted = ? and merchant.id = ? and receiveStatus= ?  and prizeProduct.customActivity.prizeCancelType = ?" ,
                DeletedStatus.UN_DELETED , merchantId , receiveStatus , prizeCancelType).fetch();
    }


    /**
     * 加载某一用户的所有抽到的奖品
     * @param userId
     * @return
     */
    public static List<UserPrizes> loadByUserId(long userId){
        return UserPrizes.find(" weixinUser.id = ? and deleted = ? order by receiveStatus desc , createAt desc" , userId , DeletedStatus.UN_DELETED).fetch();
    }


}
